/* This file is part of GSMC.
 * 
 * GSMC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GSMC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GSMC.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.hpxn.gsmc.core.event;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.hpxn.gsmc.config.map.Map.Mapping;
import net.hpxn.gsmc.config.map.Map.Mapping.Parameter;
import net.hpxn.gsmc.core.server.Server;

import org.apache.commons.lang.StringUtils;

/**
 * Represents an event received from the game server.
 * 
 * @author peter
 * 
 */
public class Event extends BaseEvent {

	public Event( String pEvent, Server pServer ) {
		super(pEvent, pServer);
	}

	/**
	 * Returns the event string with the time removed and if the event is a
	 * player triggered event, the player info is removed also.
	 * 
	 * @return String
	 */
	@Override
	public String stripPlayerInfo( String pRawEvent ) {
		String wEventNoTime = stripDateTime( pRawEvent );
		if ( !getPlayerMap().isEmpty() ) {
			return wEventNoTime
					.substring( wEventNoTime.indexOf( "\"", 15 ) + 2,
							wEventNoTime.length() );
		}
		return wEventNoTime;
	}

	@Override
	public String stripDateTime( String pEvent ) {
		return pEvent
				.substring( pEvent.indexOf( ":", 26 ) + 2, pEvent.length() );
	}

	/**
	 * Returns a HashMap of parameters described in the mapping file for this
	 * event. The name of the parameter is the key and the data from the event
	 * is the value. To check which parameters are being set, refer to the
	 * mapping file being used.
	 * 
	 * If this is a startup action the parameter map will be empty.
	 * 
	 * @return HashMap<String, String>
	 * @throws Exception 
	 */
	@Override
	public HashMap<String, String> getParameterMap() {
		if ( parameterMap != null ) {
			return parameterMap;
		}

		parameterMap = new HashMap<String, String>();
		for ( Mapping wMapping : rawMap.getMapping() ) {
			int count = 1;
			String wTrigger = StringUtils.chomp( wMapping.getTrigger().trim() );
			Matcher wMatcher = Pattern.compile( wTrigger,
					Pattern.CASE_INSENSITIVE ).matcher(
					stripPlayerInfo( rawEvent ) );
			if ( wMatcher.lookingAt() ) {
				for ( Parameter wParameter : wMapping.getParameters() ) {
					parameterMap.put( wParameter.getValue(),
							wMatcher.group( count++ ) );
				}
				break;
			}
		}
		return parameterMap;
	}

	/**
	 * Returns a HashMap containing information about the player that caused the
	 * event. If the event is a non-player event then an empty HashMap will be
	 * returned.
	 * 
	 * @return HashMap<String, String>
	 * @throws Exception 
	 */
	@Override
	public HashMap<String, String> getPlayerMap() {
		if ( playerMap != null ) {
			return playerMap;
		}
		
		playerMap = new HashMap<String, String>();
		Matcher wMatcher = Pattern.compile( rawMap.getPlayer() ).matcher(
				stripDateTime( rawEvent ) );

		if ( wMatcher.lookingAt() ) {
			playerMap.put( "name", wMatcher.group( 1 ) );
			playerMap.put( "id", wMatcher.group( 2 ) );
			playerMap.put( "steamid", wMatcher.group( 3 ) );
			playerMap.put( "team", wMatcher.group( 4 ) );
		}

		return playerMap;
	}
}
